﻿$(document).ready(function () {

    //菜单树
    GetSmJSON2(CLDJServiceUrl + "/SmwsFrame/GetAllMenu", "", function () {
    	    if (xmlhttp.readyState==4)
						             {// 4 = "loaded"
						                if (xmlhttp.status==200)
						                 {// 200 = "OK"
						                    var txt= parseResponseText(xmlhttp.responseText)
						                     if (txt!= "") {
						                    	 var d=JSON.parse(txt);
                                $("#menutree").combotree('loadData',d );
                            }
						                }
						                }
    
    });
    //图标树
//    GetSmJSON(CLDJServiceUrl + "/SmwsFrame.asmx/GetMenuIcons", "{}", function (d) {
//        if (d != "") {
//            $("#icontree").combotree('loadData', JSON.parse(d));
//        }
//    });

    var myMenu = new SmObjectData("BASE_MENU");

    $('#menugrid').treegrid({
        title:"模块管理",
        idField: 'MENU_ID',
        treeField: 'MENU_NAME',
        columns: [[
            { field: 'MENU_NAME', title: '模块', width: 250 },
            { field: 'LINK_URL', title: '访问地址', width: 300 },
//            { field: 'ICON', title: '图标', width: 80 },
//            { field: 'BZ', title: '模块说明', width: 80 },
            { field: 'ORDER_NUM', title: '显示序号', width: 100 }
        ]],
        toolbar:
            [{
                text: '新增',
                iconCls: 'icon-add',
                handler: function () {
                    myMenu.Init("MENU_ID", "");
                    GetValues();
                    $("#menudlg").dialog("open");
                }
            }, '-', {
                text: '修改',
                iconCls: 'icon-edit',
                handler: function () {
                    var node = $('#menugrid').treegrid('getSelected');
                    if (node == null) {
                        $.messager.alert('提示', '请选择一行进行修改。');
                    } else {
                        myMenu.Init("MENU_ID", node.MENU_ID);
                        GetValues();
                        $("#menudlg").dialog("open");
                    }
                    
                }
            }, '-', {
                text: '删除',
                iconCls: 'icon-remove',
                handler: function () {
                    var node = $('#menugrid').treegrid('getSelected');
                    if (node == null) {
                        $.messager.alert('提示', '请选择一行进行修改。');
                    } else {
                        $.messager.confirm('警告', '确认要删除选中的菜单及其子菜单吗?', function (r) {
                            if (r) {
                                GetSmJSON2(CLDJServiceUrl + "/SmwsFrame/DeleteMenu", "{'bsm':'"+node.BSM+"'}", function () {
                                	 if (xmlhttp.readyState==4)
						             {// 4 = "loaded"
						                if (xmlhttp.status==200)
						                 {// 200 = "OK"
						                   
                                  reloadgrid();
                            
						                }
						                }
                                  
                                });
                            }
                        });

                    }
                }
            }, '-', {
                text: '刷新',
                iconCls: 'icon-reload',
                handler: function () {
                    reloadgrid();
                }
            }]
    });

    reloadgrid();

    function reloadgrid() {    
        GetSmJSON2(CLDJServiceUrl + "/SmwsFrame/GetMenuTreeGrid", "", function () {
        		 if (xmlhttp.readyState==4)
						             {// 4 = "loaded"
						                if (xmlhttp.status==200)
						                 {// 200 = "OK"
						                                  var txt= parseResponseText(xmlhttp.responseText)
						                     if (txt!= "") {
                                $('#menugrid').treegrid('loadData', JSON.parse(txt));
                            }                            
						                }
						                }
  
        });
    }

    function GetValues() {
        $("#menuName").val(myMenu.GetValue("MENU_NAME"));
        $("#menutree").combotree('setValue', myMenu.GetValue("PARENT_MENU_ID"));
        $("#menuUrl").val(myMenu.GetValue("LINK_URL"));
//        $("#icontree").combotree('setValue', myMenu.GetValue("ICON"));
//        $("#menuBZ").val(myMenu.GetValue("BZ"));
        $("#menuXH").val(myMenu.GetValue("ORDER_NUM"));
    }
    function SetValues() {
        myMenu.SetValue("MENU_NAME", $("#menuName").val());
        myMenu.SetValue("PARENT_MENU_ID", $("#menutree").combotree('getValue'));
        myMenu.SetValue("LINK_URL", $("#menuUrl").val());
//        myMenu.SetValue("ICON", $("#icontree").combotree('getValue'));
//        myMenu.SetValue("BZ", $("#menuBZ").val());
        myMenu.SetValue("ORDER_NUM", $("#menuXH").val());
    }

    $("#btnSave").click(function () {
        SetValues();
        myMenu.Update();
        reloadgrid();
        $("#menudlg").dialog("close");
    });

    $("#btnCance").click(function () {
        $("#menudlg").dialog("close");
    });


});